package com.alex.goods.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alex.goods.util.Blank;
import com.alibaba.fastjson2.JSON;

public class BasicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String action = request.getParameter("action");
		
		Method[] methods = this.getClass().getDeclaredMethods();
		Method method = null;
		for (Method m : methods) {
			if (m.getName().equals(action)) {
				method = m;
				break;
			}
		}
		try {
			invokeMethod(method, request, response);
		} catch (ParseException e) {

		}
	}
	
	private void invokeMethod(Method m, HttpServletRequest request, HttpServletResponse response)
			throws ParseException {
		Parameter[] parameters = m.getParameters();
		Class<?>[] types = m.getParameterTypes();
		Object[] params = new Object[types.length];
		for (int i = 0; i < types.length; i++) {
			Parameter p = parameters[i];
			if (types[i].equals(HttpServletRequest.class)) {
				params[i] = request;
			} else if (types[i].equals(HttpServletResponse.class)) {
				params[i] = response;
			} else {
				

				String value = request.getParameter(p.getName());
			    if(Blank.isBlank(value)) {
					params[i] = value;
					continue;
				}
				
				if (types[i].equals(Date.class)) {
					
					SimpleDateFormat sdf = new SimpleDateFormat();
					params[i] = sdf.parse(value);
					
				} else if (types[i].equals(Integer.class)) {
					
					params[i] = Integer.valueOf(value);
					
				} else if (types[i].equals(String.class)) {
					
					params[i] = value;
				}
				
				// ................
			}
		}
		try {
			
			Object result = m.invoke(this, params);
			
			if (result != null) {
				response.setContentType("application/json;charset=utf-8");
				
				// if this not have utf-8 to json then the page will think this json only is
				// characters
				// then the page will not use json as a instance or class
				String str = JSON.toJSONString(result); // in this json use class's get function to do match
				// if you have getT then json will has { t:string }
				PrintWriter pw = response.getWriter();
				pw.write(str);
				
				//System.out.println("basic ");
				System.out.println(str);
				
				
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}
	
	

	
	
	public Object makeObject(HttpServletRequest request, Class<?> type) {
		Object o = null;
		try {
			o = type.newInstance();
			Enumeration<String> names = request.getParameterNames();
			while (names.hasMoreElements()) {
				String name = names.nextElement();
				String value = request.getParameter(name);
				try {
					Field f = type.getDeclaredField(name);
					f.setAccessible(true);
					if (f.getType().equals(String.class)) {
						f.set(o, value);
					} else if (f.getType().equals(Integer.class)) {
						if (value != null && !"".equals(value)) {
							f.set(o, Integer.valueOf(value));
						}
					} else if (f.getType().equals(Date.class)) {
						if (value != null && !"".equals(value)) {
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
							f.set(o, sdf.parse(value));
						}
					}
				} catch (Exception e) {
				}
			}
		} catch (Exception e) {
			
		}
		return o;
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
